﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Portal.Web.Model;
using Portal.Cms.Model;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Cms.Utility;
using Portal.Systems.Model;

namespace Portal.Cms.Render
{
    public class CategoryPermissionRenderBll
    {
        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }

        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
            
                string UserType = (string)WebEnvironments.Request(SiteId, "type");
                if (string.IsNullOrEmpty(UserType)) UserType = "";
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function ClientReading()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        UserType = document.getElementById('txtUserType').value;\r\n" +
                   "        Keyword = document.getElementById('txtKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.CategoryPermissionRenderBll.ServerSideReading(SiteId, UserType, Keyword).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListRoles').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +
                   "</script>\r\n" +
                   "<div id=divActionForm></div>\r\n" +
                   "<input type=hidden id=txtUserType value=\"" + UserType + "\">\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                  // "            <li onclick=\"javascript:ClientAdd();\"><img class=image src=\""+ButtonIconRoot+"/Add.png\"><div>Tạo mới</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=admin','_self');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "    <div class=CssListCategoryPermission>\r\n"+
                   "        <table>\r\n"+
                   "        <tr>\r\n"+
                   "            <td class=col1>\r\n"+
                   "                <div class=CssUlAccountType>\r\n"+
                                        ServerReadingAccountType(SiteId, UserType).HtmlContent +
                   "                </div>\r\n"+
                   "            </td>\r\n"+
                   "            <td class=col2>\r\n" +
                   "                   <div class=CssFilter>Từ khóa lọc:<input type=textbox class=textbox id=txtKeyword><input type=button class=button value=\"Lọc theo từ khóa\" onclick=\"javascript:ClientReading();\"></div>\r\n" +
                   "                   <div id=\"divListRoles\" class=tabcontent>\r\n" +
                   ServerSideReading(SiteId,UserType,"").HtmlContent+
                   "                   </div>\r\n" +
                   "            </td>\r\n"+
                   "        </tr>\r\n"+
                   "        </table>\r\n"+
                   "    </div>\r\n"+
                   "</div>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>"+ ex.Message.ToString()+"</div>\r\n";
            }
            
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerReadingAccountType(string SiteId,string AccountType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonUrl = WebConfig.GetIconRoot(OSiteParam);
                string AllAppendClass = "";
                if (string.IsNullOrEmpty(AccountType))
                {
                    AllAppendClass = " class=active";
                }
                AccountTypeCls[] AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId,"");
                
                string Html =
                    "<ul>\r\n"+
                    " <li " + AllAppendClass + " onclick=\"javascript:window.open('CategoryPermission.aspx','_self');\">" +
                    "   <div><img class=imageicon src=\""+ButtonUrl+"/AccountType/all.png\"></div>\r\n" +
                    "   <div>Toàn bộ</div>\r\n" +
                    "</li>\r\n";
                for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                {
                    string AppendClass = "";
                    if (AccountTypes[iIndex].AccountType.Equals(AccountType))
                    {
                        AppendClass = " class=active";
                    }
                    Html +=
                        " <li " + AppendClass + " onclick=\"javascript:window.open('CategoryPermission.aspx?type=" + AccountTypes[iIndex].AccountType + "','_self');\">" + 
                        "   <div><img class=imageicon src=\""+ButtonUrl+"/AccountType/"+AccountTypes[iIndex].AccountType+".png\"></div>\r\n"+
                        "   <div>"+AccountTypes[iIndex].Description + "</div>\r\n"+
                        "</li>\r\n";
                }
                Html +=
                   "    </ul>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId, string UserType, string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {

                RoleCls[] Roles = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRoles(SiteId, UserType, Keyword);
                string Subject = "Danh sách nhóm truy cập";
                if (!string.IsNullOrEmpty(UserType))
                {
                    AccountTypeCls AccountType = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateAccountTypeModel(SiteId, UserType);
                    Subject = "Danh sách - " + AccountType.Description;
                }
                string Html =
                   "        <h3>Danh sách nhóm truy cập</h3>\r\n" +
                   "            <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th class=thIndex>#</th>\r\n" +
                   "                    <th class=thCode>Mã</th>\r\n" +
                   "                    <th>Nhóm truy cập</th>\r\n" +
                   //"                    <th>Kiểu nhóm</th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = 0; iIndex < Roles.Length; iIndex++)
                {
                    Html +=
                       "    <tr>\r\n" +
                       "        <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                       "        <td class=tdCode>" + Roles[iIndex].RoleCode + "</td>\r\n" +
                       "        <td>" + Roles[iIndex].RoleName + "</td>\r\n" +
                       //"        <td>" + Roles[iIndex].RoleTypeText + "</td>\r\n" +
                       "        <td style=\"width:10px;\"><input type=button class=button value=\"Chuyên mục\" onclick=\"window.open('CategoryPermissionConfig.aspx?RoleId=" + Roles[iIndex].RoleId + "','_self');\"></td>\r\n" +
                       "   </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
